//
//  ReadAddInfoViewController.m
//  WYnewsss
//
//  Created by lanou on 16/4/25.
//  Copyright © 2016年 lanou. All rights reserved.
//

#import "ReadAddInfoViewController.h"
#import "NetWorkrequestManager.h"
#import "ReadInfoModel.h"
#import "BaseTableViewCell.h"
#import "FactoryTableViewCell.h"
#import "Const.h"
#import "ReadInfoView.h"
#import<UIImageView+WebCache.h>
#import <MJRefresh.h>
#define ScreenW [UIScreen mainScreen].bounds.size.width
#define ScreenH [UIScreen mainScreen].bounds.size.height
#define TopicDetailImgH 200
#define TopicDetailInsH 136
#define TopicTopViewH 50

@interface ReadAddInfoViewController ()<UITableViewDelegate,UITableViewDataSource>
@property(nonatomic,strong)NSMutableArray *infoArray;
@property(nonatomic,assign)NSInteger size;
@property(nonatomic,assign)NSInteger count;
@property (weak, nonatomic) IBOutlet UITableView *queAnsTableV;

/** 顶部图片 */
@property (nonatomic, weak) UIImageView *topImgV;
/** 顶部视图 */
@property (nonatomic, weak) UIView *topV;
/** 顶部标签 */
@property (nonatomic, weak) UILabel *topL;
@property (nonatomic, assign, getter=isNewQues) NSInteger newQues;
@property (nonatomic, strong) UIView *boView;
@property (nonatomic,strong)ReadInfoView *headerView;
@end

@implementation ReadAddInfoViewController
-(NSMutableArray *)infoArray{
    if (!_infoArray) {
        self.infoArray=[NSMutableArray array];
    }
    return _infoArray;
}


- (UIStatusBarStyle)preferredStatusBarStyle {
    return UIStatusBarStyleLightContent;
}
//T1458320232969
//http://c.m.163.com/nc/article/list/T1458320232969/0-20.html


-(void)requestData{
    NSString *urlStr = [NSString stringWithFormat:@"http://c.m.163.com/nc/article/list/%@/0-20.html",_tid];
    [NetWorkRequestManager requestWithType:GET urlString:urlStr parDic:@{@"count":@(_count),@"size":[NSString stringWithFormat:@"%ld",self.size+=20]} finish:^(NSData *data) {
        NSDictionary *dataDic=[NSJSONSerialization JSONObjectWithData:data options:NSJSONReadingMutableContainers error:nil];
        //NSLog(@"%@",dataDic);
        
        NSArray *infoArr =dataDic[self.tid];
        
        for (NSDictionary *dic in infoArr) {
            ReadInfoModel *infoModel=[[ReadInfoModel alloc]init];
            [infoModel setValuesForKeysWithDictionary:dic];
            // NSLog(@"%@",dic);
            [self.infoArray addObject:infoModel];
        }
        dispatch_async(dispatch_get_main_queue(), ^{
            [self.queAnsTableV reloadData];
            [self.queAnsTableV.mj_header endRefreshing];
            [self.queAnsTableV.mj_footer endRefreshing];
            
        });

    } error:^(NSError *error) {
        
    }];
}

//-(void)requsetData{
//    NSString *urlStr = [NSString stringWithFormat:@"http://c.m.163.com/nc/article/list/%@/0-20.html",_tid];
//    [NetWorkrequestManage requestWithType:GET url:urlStr parameters:@{@"count":@(_count),@"size":[NSString stringWithFormat:@"%ld",self.size+=20]} finish:^(NSData *data) {
//        [self.infoArray removeAllObjects];
//        NSDictionary *dataDic=[NSJSONSerialization JSONObjectWithData:data options:NSJSONReadingMutableContainers error:nil];
//        //NSLog(@"%@",dataDic);
//        
//        NSArray *infoArr =dataDic[self.tid];
//        
//        for (NSDictionary *dic in infoArr) {
//            ReadInfoModel *infoModel=[[ReadInfoModel alloc]init];
//            [infoModel setValuesForKeysWithDictionary:dic];
//           // NSLog(@"%@",dic);
//            [self.infoArray addObject:infoModel];
//        }
//        dispatch_async(dispatch_get_main_queue(), ^{
//          [self.queAnsTableV reloadData];
//            [self.queAnsTableV.mj_header endRefreshing];
//            [self.queAnsTableV.mj_footer endRefreshing];
//           
//        });
//    } error:^(NSError *error) {
//        
//    }];
//}

- (void)viewDidLoad {
    [super viewDidLoad];
    _count=0;
   _size=20;
    [self requestData];
    [_queAnsTableV registerNib:[UINib nibWithNibName:@"ReadInfoViewCell" bundle:nil] forCellReuseIdentifier:NSStringFromClass([ReadInfoModel class])];
    //创建头部视图
    NSArray *views =[[NSBundle mainBundle]loadNibNamed:@"ReadInfoView" owner:nil options:nil];
    self.headerView=[views lastObject];
    self.headerView.frame=CGRectMake(0, 0, ScreenW,150);
    //[_queAnsTableV setTableHeaderView:_headerView];
    //_queAnsTableV.tableHeaderView=_headerView;
    
    self.navigationController.hidesBarsOnSwipe=YES;

//    self.queAnsTableV.mj_header= [MJRefreshNormalHeader headerWithRefreshingTarget:self refreshingAction:@selector(bottomDragRefreshData)];
//    self.queAnsTableV.mj_footer = [MJRefreshAutoNormalFooter footerWithRefreshingTarget:self refreshingAction:@selector(topDragRefreshData)];
//    [self.queAnsTableV.mj_header beginRefreshing];
    
    
    // Do any additional setup after loading the view from its nib.
}
-(void)bottomDragRefreshData{
    [self requestData];
    
}
-(void)topDragRefreshData{
    [self requestData];
}
-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    return self.infoArray.count;
}
-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
    return 120;
}
-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    ReadInfoModel*model=_infoArray[indexPath.row];
    BaseTableViewCell *cell =[FactoryTableViewCell creatTableViewCell:model withTableView:tableView andIndexPath:indexPath];
    [cell setDataWithModel:model];
    cell.selectionStyle =UITableViewCellSelectionStyleNone;
    return cell;
}
- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
